/*
 飞机大战  BY  gaolk 神话
 * */
var plane={
	
	CreatMyPlane:function(){
		var MyPlane='<div id="js_myplane"></div>';
		$('#js_plane').append(MyPlane);
		this.MyPlane=$('#js_myplane');
		this.MyPlaneX=11;
		this.MyPlaneY=22;
		for(var i=0;i<4;i++){
			this.Map[22+i][11+i]='m';
		}
		var _that=this;
		$(window).keydown(function(e){
			switch(e.keyCode){
				case 38:
				  _that.move('up');
				  break;//上
				case 40:
				  _that.move('down');
				  break;//下
				case 37:
				  _that.move('left');
				  break;//左
				case 39:
				  _that.move('right');
				  break;//右
				 default:
				 break;
				 
			}
		})
	},//初始化飞机
	
	CreatAttackPlane:function(){
		var attackx=parseInt(Math.random()*(this.cols-3));
		var attacky=0;
		var _that=this;
		if(this.Map[attacky][attackx]!='m'){
			var AttackPlane='<div class="attackplane" style="left:'+attackx*20+'px"></div>';
			$('#js_plane').append(AttackPlane);
			for(var i=0;i<3;i++){
				for(var t=0;t<3;t++){
					this.Map[attacky+i][attackx+t]='a';
					//console.log(this.Map[attacky+i][attackx+i]+'y:'+(attacky+i)+',x:'+(attackx+t))
				}
			}
			
			setTimeout(function(){_that.CreatAttackPlane()},1500);
		}else{
			this.GameOver();
		}
		
			_that.AttackRun($('.attackplane:last'),attackx,attacky);
		
		
	},//初始化敌机
	
	move:function(place){
		var PlanePos=this.MyPlane.position();
		switch(place){
			case 'left':
			if(PlanePos.left-20>=0){
				this.MyPlane.css('left',PlanePos.left-20);
				this.run(-1,0);
			}
			break;
			case 'right':
			if(PlanePos.left+20<=(this.x-80)){
				this.MyPlane.css('left',PlanePos.left+20);
				this.run(1,0);
			}
			break;
			case 'up':
			if(PlanePos.top-20>=0){
				this.MyPlane.css('top',PlanePos.top-20);
				this.run(0,-1);
			}
			break;
			case 'down':
			if(PlanePos.top+20<=(this.y-80)){
				this.MyPlane.css('top',PlanePos.top+20);
				this.run(0,1);
			}
			break;
			default :
			break;
			
		}
		
	}, //移动飞机方向
	
	run:function(x,y){
		var Mx=this.MyPlaneX+x;
		var My=this.MyPlaneY+y;
		console.log(this.Map[My][Mx]+'y:'+My+',x:'+Mx);
		if(x==-1){
			 for(var i=0;i<4;i++){
			 	if(this.Map[My+i][Mx]=='a'){
			 		this.GameOver();
			 		break;
			 	}
			 }
			for(var i=0;i<4;i++){
				this.Map[My+i][Mx]='m';
			}
			for(var i=0;i<4;i++){
				this.Map[My+i][Mx+4]=0;
			}
		}
	   else if(x==1){
	   	    for(var i=0;i<4;i++){
			 	if(this.Map[My+i][Mx+3]=='a'){
			 		this.GameOver();
			 		break;
			 	}
			 }
	   	    for(var i=0;i<4;i++){
				this.Map[My+i][Mx+3]='m';
			}
			for(var i=0;i<4;i++){
				this.Map[My+i][Mx-1]=0;
			}
	   }
	   else if(y==-1){
	   	    for(var i=0;i<4;i++){
			 	if(this.Map[My][Mx+i]=='a'){
			 		this.GameOver();
			 		break;
			 	}
			 }
	   	    for(var i=0;i<4;i++){
				this.Map[My][Mx+i]='m';
			}
			for(var i=0;i<4;i++){
				this.Map[My+4][Mx+i]=0;
			}
	   }
	   else if(y==1){
	   	    for(var i=0;i<4;i++){
			 	if(this.Map[My+3][Mx+i]=='a'){
			 		this.GameOver();
			 		break;
			 	}
			 }
	   	    for(var i=0;i<4;i++){
				this.Map[My][Mx+i]='m';
			}
			for(var i=0;i<4;i++){
				this.Map[My-1][Mx+i]=0;
			}
	   }
	   
		
			this.MyPlaneY=My;
			this.MyPlaneX=Mx;
		
	   
	},//检测碰撞
	
	AttackRun:function(obj,x,y){
		var _that=this;
		var RunSet;
	   obj.animate({top:obj.position().top+20},100,function(){
	   	    var Ax=x;
	        var Ay=y+1;
	        for(var i=0;i<3;i++){
			 	if(_that.Map[Ay+3][Ax+i]=='m'){
			 		_that.GameOver();
			 		break;
			 	}
			 }
	   	    for(var i=0;i<3;i++){
				_that.Map[Ay][Ax+i]='a';
			}
			for(var i=0;i<3;i++){
				_that.Map[Ay-1][Ax+i]=0;
			}
			if(Ay>=(_that.rows-4)){
				for(var i=0;i<3;i++){
					for(var t=0;t<3;t++){
						_that.Map[Ay+i][Ax+t]=0;
					}
				}//清空敌机坐标
				obj.remove();
				clearTimeout(RunSet);
				
			}else{
				RunSet=setTimeout(function(){_that.AttackRun(obj,Ax,Ay)})
			}
			
	   });
	        
	},//检测敌机碰撞
	
	GameOver:function(){
	   if(confirm('GAME OVER,在来一次？')){
	   	 window.location.reload();
	   }
	},
	
	show:function(x,y){
		this.x=x;
		this.y=y;
		this.Map=[];
		this.cols=x/20;
		this.rows=y/20;
		for(var y=0;y<this.rows;y++){
			this.Map[y]=[];
			for(var x=0;x<this.cols;x++){
				this.Map[y][x]=0;
			}
		}
		this.CreatMyPlane();//初始化飞机
		this.CreatAttackPlane();//初始化敌机
	}
}


